package com.ssm.demo.service.controller;

import com.ssm.demo.service.service.IStudentService;
import com.ssm.demo.service.view.vo.StudentReqVO;
import com.ssm.demo.service.view.vo.StudentVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author Olsond
 * @date 2023/10/15
 * @description
 */
@RestController
@RequestMapping("/demo")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class DemoController {

    public final IStudentService studentService;

    @GetMapping(value = "/id", produces = MediaType.TEXT_PLAIN_VALUE)
    public String testDemo(String id, String name) {
        System.out.println("get id" + id + " and " + name);
        return "hello world";
    }

    @GetMapping(value = "/id1")
    public ModelAndView testDemo1(String id, String name) {
        System.out.println("get id" + id + " and " + name);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("hello");
        return modelAndView;
    }

    @PostMapping(value = "/student", produces = MediaType.APPLICATION_JSON_VALUE)
    public String saveStudent (@RequestBody StudentVO studentVO) {
        studentService.saveStudent(studentVO);
        return "保存成功";
    }

    @DeleteMapping(value = "/student/no")
    public String deleteStudent(String studentNo) {
        studentService.deleteStudent(studentNo);
        return "删除成功";
    }

    @PutMapping(value = "/student/no")
    public String updateStudent(@RequestBody StudentVO studentVO) {
        studentService.updateStudent(studentVO);
        return "修改成功";
    }

    @GetMapping(value = "/student", produces = MediaType.APPLICATION_JSON_VALUE)
    public List<StudentVO> selectStudent (StudentReqVO studentReqVO) {
        return  studentService.queryStudent(studentReqVO);
    }

}
